import fetch from 'dva/fetch';
import * as api from '../../services/Work/relation';
import _ from 'lodash';

export default {
  namespace: 'relation',
  state: {
    loading: false,
    info: {},
  },
  subscriptions: {
    setup({ dispatch, history }) {},
  },
  effects: {
    *fetch({ payload }, { put, call, select }) {
      yield put({ type: 'changeOrgInfoLoading', payload: false });
      const params = {
        code:
          !_.isEmpty(payload.code) ?
          payload.code :
          yield select(state => state.user.current_org.org_code),
        zb_code:
          !_.isEmpty(payload.zb_code) ?
          payload.zb_code :
          yield select(state => state.user.current_org.org_zb_code)
      };
      const { data: { ret: data } } = yield call(api.getOrgInfo, params);
      yield put({ type: 'saveOrgInfo', payload: data });
      yield put({ type: 'changeOrgInfoLoading', payload: true });
    }
  },
  reducers: {
    changeOrgInfoLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
    saveOrgInfo(state, action) {
      return {
        ...state,
        info: action.payload,
      };
    },
  },
};
